﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ADOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ADUser"] == null)
        {
            Response.Redirect("Advertisement.aspx");
        }
        this.SqlDataSource1.SelectCommand = "SELECT top 1 [RemainMoney],[Cent] FROM [GowllUsers] WHERE [ID]=" + Session["ADUserID"];
        this.Repeater1.DataBind();
        this.SqlDataSource2.SelectCommand = "SELECT [ID], [AdInfo], [Price], [StartDate],[ShowCount] FROM [GowllADOrders] WHERE [Payed]=0 and [UserID]=" + Session["ADUserID"] + " ORDER BY [ID] DESC ";
        this.GridView1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.SqlDataSource2.DeleteCommand = "DELETE FROM [GowllADOrders] WHERE [ID]=" + ((LinkButton)sender).CommandName;
        this.SqlDataSource2.Delete();
    }
    protected void LinkButton1_Init(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return(confirm('确定要取消订单吗？'))";
    }
    protected void LinkButton2_Init(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return(confirm('确定要支付该订单吗？点击“确定”将从您的社区余额中扣出费用。'))";
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string ID = ((LinkButton)sender).CommandName;
        this.SqlDataSource2.DeleteCommand = "DECLARE @PRICE int;DECLARE @UserID int;SELECT @UserID=[UserID],@PRICE=[PRICE] from [GowllADOrders] where [id]="
            + ID + ";update [GowllUsers] set [RemainMoney]=[RemainMoney]-@PRICE where [ID]=@UserID and [RemainMoney]>=@PRICE;";
        if (this.SqlDataSource2.Delete() < 1)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付失败，您的余额不足。');</script>");
        }
        else
        {
            this.SqlDataSource2.UpdateCommand = "update [GowllADOrders] set Payed=1 where [id]="+ID;
            this.SqlDataSource2.Update();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('支付成功。');</script>");
        }
    }
}
